package com.gyf.szcrm.web.controller;

import com.gyf.szcrm.model.CRUDResult;
import com.gyf.szcrm.model.CourseOrder;
import com.gyf.szcrm.model.PageResult;
import com.gyf.szcrm.service.CourseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/courseorder")
public class CourseOrderController {
    @Autowired
    CourseOrderService courseOrderService;
    @RequestMapping("/list")
    public String index(){
        return "courseorder/list";
    }

    @RequestMapping("/listJson")
    @ResponseBody
    public PageResult<CourseOrder> index11(CourseOrder courseOrder,int page,int limit) {
        PageResult<CourseOrder> pageResult=new PageResult<>();
        pageResult=courseOrderService.findPageResult(courseOrder,page,limit);
        return pageResult;
    }

    @RequestMapping("/toadd")
    public String toadd() {
    return "courseorder/add";
    }

    @RequestMapping("/save")
    @ResponseBody
    public CRUDResult save(CourseOrder courseOrder){
        CRUDResult crudResult=new CRUDResult();
        if(courseOrder.getOrder_id()==null||courseOrder.getOrder_id().length()==0){
            courseOrderService.save(courseOrder);
            crudResult.setSuccess(1);
            return crudResult;
        }else {
           courseOrderService.update(courseOrder);
            crudResult.setSuccess(1);
            return crudResult;
        }
    }
    @RequestMapping("/detail")
    public String detail(Model model,String order_id){
        CourseOrder courseOrder=courseOrderService.findByOrderId(order_id);
        model.addAttribute("order",courseOrder);
        return "courseorder/details";
    }
    @RequestMapping("/delete")
    @ResponseBody
    public CRUDResult deete(String order_id){
        CRUDResult crudResult = new CRUDResult();
        courseOrderService.deleteByOrderId(order_id);
            return crudResult;
    }

    @RequestMapping("/edit")
    public String edit(Model model,String order_id){
        CourseOrder courseOrder=courseOrderService.findByOrderId(order_id);
        model.addAttribute("order",courseOrder);
        return "/courseorder/edit";
    }

}
